package com.newx.open.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.newx.open.service.OpenService;
import com.newx.open.util.NewxRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yxl
 * @date 2024/7/25 0025 16:26
 */
@Service
@Slf4j
public class OpenServiceImpl implements OpenService {
    @Override
    public String generateSign(HttpServletRequest request, String appSecret) {
        // 获取参数
        String signVal = request.getParameter("app_id");
        String requestData = request.getParameter("requestData");
        // 校验参数
        Assert.notEmpty(signVal,"请求参数app_id必须");
        Assert.notEmpty(requestData,"请求参数requestData必须");
        Assert.isTrue(JSONUtil.isTypeJSONObject(requestData),"参数必须是JSON格式");
        // 获取appSecret
        Assert.notNull(appSecret,"app_secret必须");
        // 生成签名
        String signRequest = NewxRequest.signRequest(NewxRequest.getParameterMap(request), appSecret);
        return signRequest;
    }
}
